package com.better517na.elasticseach.service;

import com.alibaba.fastjson.JSONObject;
import com.better517na.elasticseach.context.ElasticSearchContext;
import com.better517na.elasticseach.context.InputParam;
import com.better517na.elasticseach.handlers.Handler;
import com.better517na.elasticseach.utils.FileUtils;
import org.springframework.stereotype.Service;

import java.util.Properties;
@Service
public class InvokeMethodService {


    private ElasticSearchContext context;

    private Properties propertiesInCache;


//    public static void main(String[] args) {
//        args = new String[1];
////        args[0]="{\"utl\":\"http://192.168.1.34\",\"port\":\"9200\",\"exeMethod\":\"import\",\"inPut\":\"D:\\esData\\beijing\\ex_input_001001001\",\"consumerIndex\":\"collection_hotel_list_001001001\",\"consumerType\":\"CollectionHotelList\",\"prefix\":\"es_exportD__out_list_001001001_\"}";
//        System.out.println("入参:"+JSONObject.toJSONString(args));
//        InvokeMethod invokeMethod = new InvokeMethod(args);
//        ElasticSearchContext context = invokeMethod.getContext();
//        Handler contextHandler = context.getHandler();
//        contextHandler.doWork();
//
//    }

    /**
     * 执行
     */
    public void excute(InputParam inputParam){
        propertiesInCache = FileUtils.getPropertiesInCache();
        initContext(inputParam);
        ElasticSearchContext context = getContext();
        Handler handler = context.getHandler();
        handler.doWork();
    }

    public void prepare(String[]  args){
        propertiesInCache = FileUtils.getPropertiesInCache();
        InputParam inputParam = null;
        if (null != args && args.length >0){
            String arg = args[0];
            inputParam = JSONObject.parseObject(arg, InputParam.class);
        }

        initContext(inputParam);
    }

    private void initContext(InputParam inputParam) {
        if (null == context){
            context = new ElasticSearchContext.ContextBuild(propertiesInCache).build(inputParam);
        }
    }

    public ElasticSearchContext getContext() {
        return context;
    }

    public void setContext(ElasticSearchContext context) {
        this.context = context;
    }

    public Properties getPropertiesInCache() {
        return propertiesInCache;
    }

    public void setPropertiesInCache(Properties propertiesInCache) {
        this.propertiesInCache = propertiesInCache;
    }
}
